package com.varton.theory;

import com.varton.annotation.Spent;

import java.lang.reflect.Method;

/**
 *
 * @author lihuazeng
 */
public class TookInvocationHandler implements InvocationHandler{
    private Object target;

    public TookInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Method targetMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
        Spent spent = targetMethod.getAnnotation(Spent.class);
        if (spent != null){
            long limit = spent.value();
            long start = System.currentTimeMillis();
            Object o = method.invoke(this.target, args);
            long end = System.currentTimeMillis();
            if((end - start) > limit){
                System.out.println("执行"+target.getClass().getName()+"的方法"+method.getName()+"超过了"+limit+"ms!");
            }
            return o;
        }else {
            return method.invoke(this.target, args);
        }
    }
}